package org.springframework.security.web.session;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.core.log.LogMessage;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

/**
 * @author Dillon
 * @date 2024/7/14
 * @slogan 致敬大师 致敬未来的你
 * @desc session 强制创建过滤器 ，每次经过请求时，手动获取以下session
 */
public class ForceEagerSessionCreationFilter extends OncePerRequestFilter {

	/**
	 * 执行过滤器
	 *
	 * @param request     请求对象
	 * @param response    响应对象
	 * @param filterChain 过滤器链
	 * @throws ServletException 执行异常
	 * @throws IOException      执行异常
	 */
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (this.logger.isDebugEnabled() && session.isNew()) {
			this.logger.debug(LogMessage.format("提取创建session"));
		}
		filterChain.doFilter(request, response);
	}

}
